﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Esb.Domain.Services
{
    public class ServiceLogService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public List<AppLogDTO> QueryServiceErrorLog(ServiceDo service, out string message)
        {
            //根据名字拉取服务log的数据
            QueryAppLogRequest logRequest = new QueryAppLogRequest();
            logRequest.AppId = "207001";//TODO：不要写死
            logRequest.FromTime = DateTime.Now.AddMinutes(-15).ToString();
            logRequest.ToTime = DateTime.Now.ToString();
            logRequest.Level = LogLevelTypeEnum.Info;
            logRequest.ClassName = "Esb Error";
            logRequest.MethodName = service.ServiceRoute;
            logRequest.PageSize = int.MaxValue;
            logRequest.Page = 1;

            RestServiceHandler rest = new RestServiceHandler();
            string logRequest2 = Helper.Helper.Serialize(logRequest);
            string response = rest.ProcessCallService(logRequest2, logUrl, "QueryAppLog");
            QueryAppLogResponse response2 = Helper.Helper.Deserialize<QueryAppLogResponse>(response);

            message = "";
            return response2.AppLogs.Where(l => l.MethodName.StartsWith(service.ServiceRoute + ".")).ToList();
        }

        public List<AppLogDTO> QueryServiceCallLog(ServiceDo service, out string message)
        {
            //根据名字拉取服务log的数据
            QueryAppLogRequest logRequest = new QueryAppLogRequest();
            logRequest.AppId = "207001";
            logRequest.FromTime = DateTime.Now.AddMinutes(-15).ToString();
            logRequest.ToTime = DateTime.Now.ToString();
            logRequest.Level = LogLevelTypeEnum.Info;
            logRequest.ClassName = "Esb Call";
            logRequest.MethodName = service.ServiceRoute;
            logRequest.PageSize = int.MaxValue;
            logRequest.Page = 1;

            RestServiceHandler rest = new RestServiceHandler();
            string logRequest2 = Helper.Helper.Serialize(logRequest);
            string response = rest.ProcessCallService(logRequest2, logUrl, "QueryAppLog");
            QueryAppLogResponse response2 = Helper.Helper.Deserialize<QueryAppLogResponse>(response);

            message = "";
            return response2.AppLogs.Where(l => l.MethodName.StartsWith(service.ServiceRoute + ".")).ToList();
        }

        /// <summary>
        /// 根据服务，服务实体，方法名称拿到日志
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="serviceInfo"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<AppLogDTO> GetLogData(DateTime startTime, DateTime endTime, string serviceInfo, string type)
        {
            //首先拉取到log数据
            //根据名字拉取服务log的数据
            QueryAppLogRequest logRequest = new QueryAppLogRequest();
            logRequest.AppId = "207001";
            logRequest.FromTime = startTime.ToString();
            logRequest.ToTime = endTime.ToString();
            logRequest.Level = LogLevelTypeEnum.Info;
            if (type == "Call")
            {
                logRequest.ClassName = "Esb Call";
            }
            else
            {
                logRequest.ClassName = "Esb Error";
            }
            logRequest.MethodName = serviceInfo;//拿到所有的log
            logRequest.PageSize = int.MaxValue;
            logRequest.Page = 1;

            RestServiceHandler rest = new RestServiceHandler();
            string logRequest2 = Helper.Helper.Serialize(logRequest);
            string response = rest.ProcessCallService(logRequest2, logUrl, "QueryAppLog");
            QueryAppLogResponse response2 = Helper.Helper.Deserialize<QueryAppLogResponse>(response);

            List<AppLogDTO> logs = response2.AppLogs;
            //TODO:拉取数据的时间范围

            return logs;
        }

        private readonly string logUrl = ConfigurationManager.AppSettings["logQueryUrl"];
    }
}
